package com.cyy.common.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.common.constant.Constants;
import com.cyy.common.mapper.UserMapper;
import com.cyy.common.mapper.WebsiteConfigMapper;
import com.cyy.common.pojo.dto.UserBasicDto;
import com.cyy.common.pojo.dto.WebsiteConfigDto;
import com.cyy.common.pojo.entity.WebsiteConfig;
import com.cyy.common.pojo.resp.WebsiteBasicInfoResp;
import com.cyy.common.service.WebsiteConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 网站配置表 服务实现类
 * </p>
 *
 * @author 曹云友
 * @since 2024-04-02
 */
@Service
@RequiredArgsConstructor
public class WebsiteConfigServiceImpl extends ServiceImpl<WebsiteConfigMapper, WebsiteConfig> implements WebsiteConfigService {


    private final UserMapper userMapper;

    @Override
    public WebsiteBasicInfoResp queryBasicInfo() {
        WebsiteBasicInfoResp resp = new WebsiteBasicInfoResp();
        // TODO 后续站长ID存到字典中，初始化加载到redis中
        // 如果用户登陆了，就使用用户的信息，没有就使用站长信息
        UserBasicDto userBasicDto = userMapper.queryUserBasicById(StpUtil.isLogin() ? StpUtil.getLoginIdAsInt() : 1);

        resp.setWebsiteAdmin(userBasicDto);

        WebsiteConfig websiteConfig = baseMapper.selectOne(Wrappers.<WebsiteConfig>lambdaQuery().last(Constants.LIMIT_1));
        WebsiteConfigDto configDto = new WebsiteConfigDto();
        BeanUtils.copyProperties(websiteConfig, configDto);
        resp.setWebsiteConfig(configDto);

        return resp;
    }
}
